/*
 * ChoosePayWayPresenter.java
 * business
 *
 * Created by chentao on 2017/5/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.choosepayway;

import android.os.Bundle;

import com.yidiandao.business.R;
import com.yidiandao.business.data.ShopPayWays;
import com.yidiandao.business.data.bean.PayWay;
import com.yidiandao.business.data.model.IConfirmOrderModel;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;
import com.yidiandao.business.utils.GsonProvider;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 选择支付方式-Presenter
 */
public class ChoosePayWayPresenter implements ChoosePayWayContract.Presenter {

    private ChoosePayWayContract.View mChoosePayWayView;

    private IConfirmOrderModel mConfirmOrderModel;

    private List<ShopPayWays> mShopPayWays;

    // 在线支付限额
    private float mOnlinePayLimit;

    public ChoosePayWayPresenter(ChoosePayWayContract.View view, IConfirmOrderModel confirmOrderModel, Bundle bundle) {
        mChoosePayWayView = view;
        mConfirmOrderModel = confirmOrderModel;

        mChoosePayWayView.setPresenter(this);
    }

    @Override
    public void start() {
        getPayWayList();
    }

    private void getPayWayList() {
        mConfirmOrderModel.getPayWayList(mChoosePayWayView.getPagerTag(), new IConfirmOrderModel.OnLoadPayWayListListener() {
            @Override
            public void onSuccess(List<ShopPayWays> payWays, float onlinePayLimit) {
                // 在线支付的限额
                mOnlinePayLimit = onlinePayLimit;

                mShopPayWays = payWays;
                mChoosePayWayView.refreshContent(SuperLoadingLayout.STATE_SUCCESS);
                mChoosePayWayView.setItems(payWays);
            }

            @Override
            public void onFailed() {
                mChoosePayWayView.refreshContent(SuperLoadingLayout.STATE_ERROR);
            }
        });
    }

    @Override
    public void confirmPayWays() {
        for (ShopPayWays shopPayWays : mShopPayWays) {
            PayWay selectedPayWay = getSelectedPayWay(shopPayWays.getPayWays());
            if (selectedPayWay != null) {
                // 保存选择的支付方式
                shopPayWays.setPayCode(selectedPayWay.getPayCode());
            }
        }

        // post to server
        // 支付方式
        String payWaysJson = GsonProvider.getExposeGson().toJson(mShopPayWays);
        mConfirmOrderModel.changePayWays(mChoosePayWayView.getPagerTag(), payWaysJson, new IConfirmOrderModel.OnChoosePayWaysListener() {
            @Override
            public void onSuccess() {
                mChoosePayWayView.finishActivity();
            }
        });
    }

    @Override
    public boolean checkOnlinePayLimit(ShopPayWays shopPayWays, PayWay payWay) {

        float otherOnlinePayTotal = getOtherOnlinePayTotal(shopPayWays);
        // 选择的是在线支付 且 与其他的在线支付金额相加超过了限额
        if (payWay.getPayCode().equals(PayWay.Flavor.ONLINE)
                && otherOnlinePayTotal + shopPayWays.getSubTotal() > mOnlinePayLimit) {

            mChoosePayWayView.showToast(UIUtils.getString(R.string.to_online_pay_headline));
            return false;
        }
        return true;
    }

    private float getOtherOnlinePayTotal(ShopPayWays chooseShopPayWays) {
        float total = 0;
        for (ShopPayWays shopPayWays : mShopPayWays) {
            PayWay selectedPayWay = getSelectedPayWay(shopPayWays.getPayWays());
            // 其他的店铺的支付方式
            // 是在线支付
            // 同时不是当前选择的店铺
            if (selectedPayWay != null
                    && selectedPayWay.getPayCode().equals(PayWay.Flavor.ONLINE)
                    && !chooseShopPayWays.equals(shopPayWays)) {
                total += shopPayWays.getSubTotal();
            }
        }

        return total;
    }

    private PayWay getSelectedPayWay(List<PayWay> payWays) {
        for (PayWay payWay : payWays) {
            if (payWay.isSelected()) {
                return payWay;
            }
        }
        return null;
    }
}
